//
//  AMDropdownMeun.m
//  AMwb
//
//  Created by zhc on 15/11/29.
//  Copyright © 2015年 zhc. All rights reserved.
//

#import "AMDropdownMeun.h"

@interface AMDropdownMeun()

@property(nonatomic,weak) UIImageView *containerView;

@end


@implementation AMDropdownMeun

//懒加载
-(UIImageView *)containerView{
    if(!_containerView){
    
        UIImageView *containerView=[[UIImageView alloc]init];
        containerView.image=[UIImage imageNamed:@"popover_background"];
        //contentView.frame=CGRectMake(60, 60, 217, 450);
        containerView.userInteractionEnabled=YES;
        containerView.width=217;
        containerView.height=217;
        
        
        
        [self addSubview:containerView];
        
        self.containerView =containerView;
    
    }

   
    return _containerView;

}


-(id)initWithFrame:(CGRect)frame{
    
    self=[super initWithFrame:frame];
    
    if(self){
       
      
        
    }
    
    return  self;
    
}

- (void)setContent:(UIView *)content{
    _content=content;
   // content.frame=CGRectMake(20,20, 0, 0);
//    
    content.x=10;
    content.y=10;
//    
   //content.width=self.containerView.width-2*content.x;
//    
    self.containerView.height=CGRectGetMaxY(content.frame)+10;
    
    self.containerView.width=CGRectGetMaxX(content.frame)+10;
// 
//  
    [self.containerView addSubview:content];

}


- (void)setContentController:(UIViewController *)contentController{

    
    _contentController=contentController;
    
    self.content=contentController.view;
    
    
    
}




+ (instancetype)menu{
    
    
    return [[self alloc]init];
    
}

- (void)showFrom:(UIView *)from{
    
    //获得窗口
    UIWindow *window=[[UIApplication sharedApplication].windows lastObject];
    //添加自己到窗口
    [window addSubview:self];
    //设置尺寸
    
    self.frame=window.bounds;
    
    //转换坐标系
    CGRect newFrame = [from convertRect:from.bounds toView:window];
    
   // CGRect newFrame1 = [from.superview convertRect:from.frame toView:window];
   
    
    self.containerView.centerX=CGRectGetMidX(newFrame);
    
    self.containerView.y=CGRectGetMaxY(newFrame);
    
    //通知外界显示了
    if([self.delegate respondsToSelector:@selector(dropdownMeunshow:)]){
        
        [self.delegate dropdownMeunshow:self];
        
    }
    




}

-(void)dismiss{


    [self removeFromSuperview];
    
    //通知外界自己被销毁了
    if([self.delegate respondsToSelector:@selector(dropdownMeunDismiss:)]){
    
        [self.delegate dropdownMeunDismiss:self];
    
    }
    
    
    

}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self dismiss];

}





@end
